package com.zhangkeyou.map.mapweb.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhangkeyou.map.mapweb.domain.entity.LoginUser;
import com.zhangkeyou.map.mapweb.domain.entity.MapData;
import com.zhangkeyou.map.mapweb.exception.DefinitionException;
import com.zhangkeyou.map.mapweb.mapper.MapDataMapper;
import com.zhangkeyou.map.mapweb.mapper.UserinfoMapper;
import com.zhangkeyou.map.mapweb.util.GPSConverterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MapDataService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MapDataService.class);

    @Autowired
    private MapDataMapper mapDataMapper;
    @Autowired
    private UserinfoMapper userinfoMapper;

    public String insertMapData(MapData mapData){

        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(Objects.isNull(loginUser))
            throw new RuntimeException("未登录");
        String userId = loginUser.getUserinfo().getId();
        mapData.setUserinfoId(userId);
        mapDataMapper.insert(mapData);
        return mapData.getId();
    }

    public List<MapData> getMapData() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(Objects.isNull(loginUser))
            throw new RuntimeException("未登录");
        String userId = loginUser.getUserinfo().getId();
        List<MapData> mapDataList = mapDataMapper.selectByUserId(userId);
        return mapDataList;
    }

    public Long deleteMapData(String id) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(Objects.isNull(loginUser))
            throw new RuntimeException("未登录");
        String userId = loginUser.getUserinfo().getId();
        MapData mapData = new MapData();
        mapData.setId(id);
        mapData.setUserinfoId(userId);
        Long count = mapDataMapper.deleteByIdAndUserId(mapData);
        if(count==0)
            throw new DefinitionException(403,"删除数据失败");
        return count;
    }

    public Object convertGps84ToGcj02(String rings) throws Exception {

        String s = GPSConverterUtils.gps84_To_Gcj02(rings);
        if(s==null){
            throw new RuntimeException("格式错误");
        }
        return JSON.parse(s);
    }
    public Object convertGcj02ToGps84(String rings) throws Exception {
        String s = GPSConverterUtils.gcj02_To_Gps84(rings);
        if(s==null){
            throw new RuntimeException("格式错误");
        }
        return JSON.parse(s);
    }
    public int insertPath(MapData mapData){

        return 1;
    }

    /**
     * 添加路径跟踪的点
     * @param mapData
     * @return
     */
    public Object addPath(MapData mapData) {
        MapData mapDataInDatabase = mapDataMapper.selectMapDataById(mapData.getId());
        if(Objects.isNull(mapDataInDatabase)){
            throw new RuntimeException("未找到id的mapData"+mapData.getId());
        }
        String dataInDatabaseString = mapDataInDatabase.getData();
        JSONObject dataInDatabaseJSONObject = (JSONObject) JSON.parse(dataInDatabaseString);
        JSONArray pathInDatabase = dataInDatabaseJSONObject.getJSONArray("path");

        JSONArray pathInUpload = ((JSONObject)JSON.parse(mapData.getData())).getJSONArray("path");
        pathInDatabase.addAll(pathInUpload);
        dataInDatabaseJSONObject.put("path",pathInDatabase);

        int count = mapDataMapper.updateMapdataData(mapData.getId(),dataInDatabaseJSONObject.toString());


//        dataInDatabaseJSONObject.get
        return count;
    }

    /**
     * 更新所有data
     * @param mapData
     * @return
     */
    public Object updateDate(MapData mapData){
        int count = mapDataMapper.updateMapdataData(mapData.getId(),mapData.getData().toString());
        return count;
    }
}
